//导入配置文件
const {DBHOST,DBPORT} = require('./config')
module.exports = function(dbname,success,error){
    if(typeof error !== 'function'){
        error = ()=>{
            console.log('连接失败');
        }
    }
    const mongoose = require('mongoose');
    //连接数据库
    mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${dbname}`)
    //设置回调
    mongoose.connection.once('open',()=>{
        success()
    })
    mongoose.connection.on('error',()=>{
        error()
    })
    mongoose.connection.on('close',()=>{
        console.log('连接失败');
    })
}
